package uit.shoeshop.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import uit.shoeshop.lib.Util;
import uit.shoeshop.model.CheckoutModel;
import uit.shoeshop.model.PaymentModel;
import uit.shoeshop.dto.CityDTO;
import uit.shoeshop.dto.ProductDTO;

import java.util.ArrayList;
import java.util.HashMap;

public class CheckoutAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private ArrayList<CityDTO> Cities;
	private HashMap<Integer,Integer> cart;
	private HashMap<Integer,ProductDTO> products;
	private Integer totalPrice;
		
	private String company;
	private String fullname;
	private String address;
	private String phone;
	private String city;
	private String comment;
	
	private String pid;
	private String pqty;
	private String paction;
	private String perror;
	private String er;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
    public String payment() throws Exception {
    	String ret = NONE;
    	PaymentModel payment = new PaymentModel();
    	CheckoutModel checkout = new CheckoutModel();
    	setCart(checkout.getRawCart(request));
    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST")){
        		if(getFullname() != null && getAddress() != null && Util.isIntNumber(getPhone()) && getCart() != null && getCart().size() > 0){
            		int checkInsert =  payment.insertBill(getCart(), getCompany(), getFullname(), getAddress(), getPhone(), getCity(), getComment());
            		if(checkInsert == -1)
            			return ERROR;
            		else if(checkInsert == 0)
            			response.sendRedirect("checkout.html?er=1");
            		else{
            			Util.deleteSessions("cart", request);
            			return SUCCESS;
            		}
        		}
        	}    		
    		setCities(payment.getAllCity());
    		if(getCities() == null || getCities().size() == 0){
    			ret = ERROR;
    		}
    	}catch(Exception e){
    		ret = ERROR;
    	}
    	
        return ret; //SUCCESS- Khi payment thanh cong
    }
  
    public String execute() throws Exception {
    	setPerror(null);
    	CheckoutModel checkout = new CheckoutModel();
    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST") && getPaction() != null&& Util.isIntNumber(getPid()) && Util.isIntNumber(getPqty())){
        		if(getPaction().equals("change")){
        			int check = checkout.changeQty(Integer.parseInt(getPid()),Integer.parseInt(getPqty()), request);
        			if(check == -1)
        				return ERROR;
        			else if(check == 0)
        				setPerror("Số lượng sản phẩm không đủ !");
        		}else{
        			if(checkout.deleteProductInCart(Integer.parseInt(getPid()), request) == false)
        				return ERROR;    			
        		}
        	}
        	if(getEr() != null){
        		setPerror("Số lượng sản phẩm không đủ !");
        		setEr(null);
        	}
    		setCart(checkout.getRawCart(request));
    		if(getCart() != null && getCart().size() > 0){
    			setProducts(checkout.getAllProductInCart(getCart()));
    			setTotalPrice(checkout.getTotalPrice());
    		}
    	}catch(Exception e){
    		return ERROR;
    	}
        return SUCCESS;
    }

	/**
	 * @param cities the cities to set
	 */
	public void setCities(ArrayList<CityDTO> cities) {
		Cities = cities;
	}

	/**
	 * @return the cities
	 */
	public ArrayList<CityDTO> getCities() {
		return Cities;
	}

	/**
	 * @param company the company to set
	 */
	public void setCompany(String company) {
		this.company = company;
	}

	/**
	 * @return the company
	 */
	public String getCompany() {
		return company;
	}

	/**
	 * @param fullname the fullname to set
	 */
	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	/**
	 * @return the fullname
	 */
	public String getFullname() {
		return fullname;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param city the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @param comment the comment to set
	 */
	public void setComment(String comment) {
		this.comment = comment;
	}

	/**
	 * @return the comment
	 */
	public String getComment() {
		return comment;
	}

	/**
	 * @param cart the cart to set
	 */
	public void setCart(HashMap<Integer,Integer> cart) {
		this.cart = cart;
	}

	/**
	 * @return the cart
	 */
	public HashMap<Integer,Integer> getCart() {
		return cart;
	}

	/**
	 * @param products the products to set
	 */
	public void setProducts(HashMap<Integer,ProductDTO> products) {
		this.products = products;
	}

	/**
	 * @return the products
	 */
	public HashMap<Integer,ProductDTO> getProducts() {
		return products;
	}

	/**
	 * @param totalPrice the totalPrice to set
	 */
	public void setTotalPrice(Integer totalPrice) {
		this.totalPrice = totalPrice;
	}

	/**
	 * @return the totalPrice
	 */
	public Integer getTotalPrice() {
		return totalPrice;
	}


	/**
	 * @param paction the paction to set
	 */
	public void setPaction(String paction) {
		this.paction = paction;
	}

	/**
	 * @return the paction
	 */
	public String getPaction() {
		return paction;
	}

	/**
	 * @param pid the pid to set
	 */
	public void setPid(String pid) {
		this.pid = pid;
	}

	/**
	 * @return the pid
	 */
	public String getPid() {
		return pid;
	}

	/**
	 * @param pqty the pqty to set
	 */
	public void setPqty(String pqty) {
		this.pqty = pqty;
	}

	/**
	 * @return the pqty
	 */
	public String getPqty() {
		return pqty;
	}

	/**
	 * @param perror the perror to set
	 */
	public void setPerror(String perror) {
		this.perror = perror;
	}

	/**
	 * @return the perror
	 */
	public String getPerror() {
		return perror;
	}

	/**
	 * @param er the er to set
	 */
	public void setEr(String er) {
		this.er = er;
	}

	/**
	 * @return the er
	 */
	public String getEr() {
		return er;
	}





}
